package com.berchina.controller;

import com.berchina.util.MapUtils;
import com.berchina.util.RedisUtil;
import com.berchina.util.SmsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.mail.Session;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 展示手机前端界面，提交订单，实现登录
 * */
@Controller
@RequestMapping("cphone")
public class CustomerPhoneController {


    @Autowired
    private RedisUtil redisUtil;

    @RequestMapping("/to_index")
    public String toIndex(){
        return "/html/customerPhone/index.html";
    }






    @RequestMapping("/getMoney")
    @ResponseBody
    public String getMoney(String lng,String lat,String oldLng,String oldLat){
        int result = (int) MapUtils.algorithm(Double.valueOf(lng), Double.valueOf(lat), Double.valueOf(oldLng), Double.valueOf(oldLat))/100;
        return result+"";
    }
    @RequestMapping("/isLogin")
    @ResponseBody
    public String isLogin(HttpSession session){
        String phone = (String) session.getAttribute("phone");
        if(StringUtils.isEmpty(phone)){
            return "error";
        }else{
            return "success";
        }
    }
    @RequestMapping("/to_login")
    public String to_login(){
        return "/html/customerPhone/html/login.html";
    }

    //=======================================================================登陆
    @RequestMapping("/login")
    @ResponseBody
    public Object login(String phone,String code,HttpSession session){
        Map<String,Object> map=new HashMap<String,Object>();
        if(StringUtils.isEmpty(phone)){
            map.put("type","error");
            map.put("msg","用户名不得为空");
            return map;
        }
        if(StringUtils.isEmpty(code)){
            map.put("type","error");
            map.put("msg","验证码不得为空");
            return map;
        }
        String rcode= (String) redisUtil.get(phone);
        if(StringUtils.isEmpty(rcode)){
            map.put("type","error");
            map.put("msg","验证码失效");
            return map;
        }else{
            if(!code.equals(rcode)){
                map.put("type","error");
                map.put("msg","验证码错误");
                return map;
            }
        }
        session.setAttribute("phone",phone);
        map.put("type","success");
        map.put("msg","登陆成功");
        return map;

    }




    @RequestMapping("/sendSms")
    @ResponseBody
    public String sendSms(String phone){

        String code = SmsUtil.getFourRandom();
//        String result = SmsUtil.sendSms(phone, code);
//        if(result.contains("OK")){
            redisUtil.setEx(phone,code,10, TimeUnit.SECONDS);
//        }else{
//            return "验证码接收失败";
//        }
        return code;
    }
}
